package gr.anagnosg.general.utils;

import gr.anagnosg.general.utils.text.Word;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    public StringUtils() {
        super();
    }
    
    public static String trimNull(Object o){
        return trimNull(o,"");
    }
    public static String trimNull(Object o,String value4Null){
        String output=null;
        if(o!=null){
            output=o.toString();    
        }
        else{
                output=value4Null;
            }
        return output;
    }
    
    public static String trimStr(String input){
        String output=input;
        if(input!=null){
            output=input.trim();
        }
        return output;
    }
    
    public static String replaceSpace(String input){
        String output=input;
        if(input!=null){
            output=input.replace(" ", "");
        }
        return output;
    }
    
    public static String trimMultiSpace(String input){
        String output=input;
        output=input.replaceAll("\\s+", " ");
        return output;
    }
    
    public static String translateCharacter(String value, String[] src, String[] dest){
        String output = null;
        for(int i  = 0 ; i< src.length ; i++){
            if(src[i].equals(value)){
                output=dest[i];
                break;    
            }
            
        }
        return output;
    }
    
    public static String[] split(String commaSeparated,boolean addEndZero){
        
        if(addEndZero){
            commaSeparated+=",0";
        }    

        String[] output= commaSeparated.split(",");
        String[] dest = new String[output.length-1];
        System.arraycopy(output, 0, dest, 0, output.length-1);
        return dest;
    }
    
    public static String lpad(String input,  int length){
        String output ="" ;
        int count = input.length() ;
        while(count<length){
            output+="0";
            count++;
        }
        output +=input;
        return output;
    }
    public static String rpad(String s, int n) {
         return String.format("%1$-" + n + "s", s);  
    }
    public static String[] getCommaSeparatedValuesWithSplit(String input,String delimenter){
        String[] output = null;
        output = input.split(delimenter); //StringTokenizer st  = new StringTokenizer(input,delimenter);
        return Arrays.copyOfRange(output, 0, output.length-1);
    }
    
    public static String[] getCommaSeparatedValuesWithSplit(String input,String delimenter,boolean removeLast){
        String[] output = null;
        output = input.split(delimenter); //StringTokenizer st  = new StringTokenizer(input,delimenter);
        int size = output.length;
        if(removeLast){size=size-1;}
        return Arrays.copyOfRange(output, 0,size);
    }
    
    public static String addQuote(String input, String character){
        return character+input+character;
    }
    
    
    public static String[] convertObj2Str(Object[] tArray){
        //Object tArray[] = allLines.toArray();
        String newArrayStrT[] = new String[tArray.length];
        for(int k = 0 ; k <tArray.length ; k ++ ){
            newArrayStrT[k]       =(String)tArray[k];
        }
        return newArrayStrT;
    }
    
    public static HashMap trimHashMapStr(HashMap hm){
        Set keys = hm.keySet();
        try{
            for (Iterator it = keys.iterator(); it.hasNext();){
                String key = (String)it.next();
                Object obj = hm.get(key);
                if(obj.getClass()==Class.forName("java.lang.String")){
                    String newTrimValue = ((String)obj).trim();
                    hm.put(key, newTrimValue);
                }
                    
            }
        }
        catch(Exception ex){
            ex.printStackTrace();    
        }
        return hm;
    }
    
    
public static List splitText2Words(String text){

    ArrayList output = new ArrayList();
    Scanner s = new Scanner(text).useDelimiter("\\s");  
    //int startIndexTotalT=0;
    int previousEndIndex = 0 ;
    //System.out.println(text);
    while( s.hasNext() ) {  
        String wordStrT = s.next();
        //System.out.println(s.next()); 
        int startIndexT= text.substring(previousEndIndex).indexOf(wordStrT)+previousEndIndex;
        int endIndex = startIndexT+wordStrT.length();
        previousEndIndex = endIndex;
        Word wt = new Word();
        wt.setText(wordStrT);
        wt.setStartIndex(startIndexT);
        wt.setEndIndex(endIndex);
        //wt.printInfo();
        output.add(wt);
    }  
    s.close();  
    
    return output;
}



public static List diffOfWords(String s1,String s2){
    List output = new ArrayList();
    diff_match_patch df = new diff_match_patch();
    LinkedList<diff_match_patch.Diff> list =df.diff_main(s1, s2);
    String previousText="";
    for(int i=0; i < list.size() ; i++){
        diff_match_patch.Diff dT = list.get(i);
        System.out.println(i+","+dT.text+","+dT.operation);
        //if(){
        
        
        //}
    }
    return output;    
}


public static char[] printStringChars(String input){
    char c[] = input.toCharArray();
    //String[] output = 
    for(int i=0;i<c.length;i++){
        if(i==0){
            System.out.print(c[i]);
        }
        else{
            System.out.print(","+c[i]);
        }
    }
    return c;
}

public static List textHasLatinChars(String text){
/*According to the documentation, \w only matches [a-zA-Z_0-9] unless you specify the UNICODE_CHARACTER_CLASS flag:
Pattern.compile("\\w+", Pattern.UNICODE_CHARACTER_CLASS)
or embed a (?U) in the pattern:
Pattern.compile("(?U)\\w+")*/
    //boolean output = false;
    List output = new ArrayList();
    Pattern pattern = Pattern.compile("[a-zA-Z]+");
    
    Matcher matcher = pattern.matcher(text);
    
      while (matcher.find()) {
      //System.out.print("Start index: " + matcher.start());
      //System.out.print(" End index: " + matcher.end() + " ");
      //System.out.print(" Word:" + text.substring(matcher.start(),matcher.end()) + "");
      //System.out.println(matcher.group());
          Word w = new Word();
          w.setStartIndex(matcher.start());
          w.setEndIndex(matcher.end());
          w.setText(matcher.group());
          output.add(w);
    }
    
    return output;
}


public static String trimSpaces(String s){
    return s.replaceAll("\\s", "");
}


}
